# To change this template, choose Tools | Templates
# and open the template in the editor.
import pygame
import random

PRESTART = 0
CALMFACE = 1
SMILE_ANIM=2
SMILING = 3
MAD_SMILE_ANIM = 4
MAD_SMILING = 5
SMILE_ANIM_BACK = 6
MAD_SMILE_ANIM_BACK = 7

SPRITESHEET_FLICKER = "../imgs/environment/slimysmiley/calmFace_Flicker.png"
SPRITESHEET_SMILE = "../imgs/environment/slimysmiley/smileyFinalCopy.png"
SPRITESHEET_SLIME = "../imgs/environment/slimysmiley/slimesheet.png"
SPRITESHEET_MASK = "../imgs/environment/slimysmiley/finalMask.png"

FLICKER_S_CHANCE = 0.05
FLICKER_L_CHANCE = 0.005
STEP_TIME = 100

FRAME_TIME = 700#250
SLIME_SPEED = 0.00075
class SlimySmiley():
    def __init__(self,loc):
        self.state = SMILING
        self.spriteSheet = {'flicker':pygame.image.load(SPRITESHEET_FLICKER) ,\
                'smile':pygame.image.load(SPRITESHEET_SMILE),
                'slime':pygame.image.load(SPRITESHEET_SLIME),
                'mask':pygame.image.load(SPRITESHEET_MASK)}

        self.spriteSheet['smile'].set_colorkey((143,50,50))
        self.spriteSheet['slime'].set_colorkey((255,255,255))
        #self.spriteSheet['slime'].set_alpha(186)
        self.spriteSheet['mask'].set_colorkey((255,255,255))

        self.drawImg = pygame.Surface((44,44))
        #self.drawImg.blit()

        self.drawImg.set_colorkey((255,255,255))
       

        self.stepc = 0
        self.drawCtr = loc
        self.animc = 0

        self.sliming = True
        self.slimeDist = 0.0
        self.frame = 0
        self.animStep = 1
        self.drawImg.blit(self.spriteSheet['smile'],(0,0),(self.frame*44,0,44,44))

    def update(self,etime):
        
        self.stepc+=etime
        if self.stepc>=FRAME_TIME:
            self.drawImg.fill((255,255,255))
            if random.random()<0.1:
                if self.animStep == 0:
                    self.animStep = random.randrange(-1,2)
            self.stepc = 0
            self.frame+=self.animStep

            if self.frame>9:
                self.frame = 9
                self.animStep = 0
                self.drawImg.blit(self.spriteSheet['smile'],(0,0),(self.frame*44,0,44,44))
            if self.frame<0:
                self.frame = 0
                self.animStep = 0
                self.drawImg.blit(self.spriteSheet['smile'],(0,0),(self.frame*44,0,44,44))
        """
        if self.stepc>STEP_TIME:
            self.drawImg.blit(self.spriteSheet['flicker'],(0,0),(0,0,44,44))
            self.stepc-=STEP_TIME
            if self.state == CALMFACE:
                self.drawImg.blit(self.spriteSheet['flicker'],(0,0),(44,0,44,44))
                rand = random.random()
                if rand<FLICKER_S_CHANCE:
                    r = (random.randrange(2))+2
                    self.drawImg.blit(self.spriteSheet['flicker'],(0,0),(r*44,0,44,44))

                if rand<FLICKER_L_CHANCE:
                    self.drawImg.blit(self.spriteSheet['flicker'],(0,0),(4*44,0,44,44))

        if self.state == SMILE_ANIM or self.state == MAD_SMILE_ANIM:
            self.animc += etime
            self.frame = int(self.animc/FRAME_TIME)
            if self.state == MAD_SMILE_ANIM and self.frame > 9:
                    self.state = MAD_SMILING
                    self.frame = 9
                    self.animc = FRAME_TIME * 9.0
            if self.state == SMILE_ANIM and self.frame > 4:
                self.state = SMILING
                self.frame = 5
                self.animc = FRAME_TIME * 5.0
            self.drawImg.blit(self.spriteSheet['smile'],(0,0),(self.frame*44,0,44,44))
            
        if self.state == SMILE_ANIM_BACK or self.state == MAD_SMILE_ANIM_BACK:
            if self.state == SMILE_ANIM_BACK and self.animc == -1:
                self.animc = 5.0 * FRAME_TIME
            elif self.state == MAD_SMILE_ANIM_BACK and self.animc == -1:
                self.animc = 9.0 * FRAME_TIME

            self.animc -= etime
            self.frame = int(self.animc/FRAME_TIME)
            if self.state == SMILE_ANIM_BACK and self.frame < 1:
                self.state = CALMFACE
                self.frame = 0
                self.animc = 0.0
            if self.state == MAD_SMILE_ANIM_BACK and self.frame < 6:
                self.state = SMILING
                self.frame = 5

        if self.state == SMILING:
            self.drawImg.blit(self.spriteSheet['smile'],(0,0),(4*44,0,44,44))

        if self.state == MAD_SMILING:
            self.drawImg.blit(self.spriteSheet['smile'],(0,0),(9*44,0,44,44))
        """
        if self.sliming:
            if self.slimeDist>= 19:
                self.slimeDist = 19
            else:
                self.slimeDist+=(SLIME_SPEED*etime)*(random.random()*0.5+0.5)
            self.drawImg.blit(self.spriteSheet['slime'],(0,20),(1*44,44-self.slimeDist,44,self.slimeDist))

            self.drawImg.blit(self.spriteSheet['mask'],(0,0),(self.frame*44,0,44,44))
            self.drawImg.blit(self.spriteSheet['slime'],(0,0),(0*44,0,44,44))
        return

    def draw(self,surf):
        drawsurf = self.drawImg
        surf.blit(drawsurf,drawsurf.get_rect(center = self.drawCtr))
        
        return